﻿using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using TeamsPortal.DataAccess;

namespace TeamsPortal.Tests.DataAccess
{
    [TestFixture]
    public class ForumsDataAccessTests : BaseRollbackTest
    {
        [Test]
        public void CountUnreadMessagesForAWriterUser()
        {
            ForumCategory category = DATestsHelper.CreateCategory("Unittestcategory");

            User writer = DATestsHelper.CreateUser("UT_writerUser", UserRole.Member);

            Forum forum = DATestsHelper.CreateForum(writer, category);
            
            Topic topic1 = DATestsHelper.CreateTopic(forum, writer, "UnittestShortname1");
            DATestsHelper.CreateMessage(topic1, writer);
            //Topic topic2 = DATestsHelper.CreateTopic(forum, writer, "UnittestShortname2");
            //DATestsHelper.CreateMessage(topic2, writer);

            List<ForumCategory> loadedList = new ForumsDataAccess().GetList(writer.Id);
            Assert.NotNull(loadedList);
            ForumCategory loadedCategory = (from c in loadedList
                                           where c.Name == category.Name
                                           select c)
                                           .FirstOrDefault();
            Assert.NotNull(loadedCategory);
            Assert.IsTrue(loadedCategory.Forums.Count == 1);

            Assert.IsTrue(loadedCategory.Forums[0].UnreadMessagesCount == 0);
        }

        [Test]
        public void CountUnreadMessagesForAReaderUser()
        {
            ForumCategory category = DATestsHelper.CreateCategory("Unittestcategory");

            User writer = DATestsHelper.CreateUser("UT_writerUser", UserRole.Member);
            User reader = DATestsHelper.CreateUser("UT_readerUser", UserRole.Member);

            Forum forum = DATestsHelper.CreateForum(writer, category);

            Topic topic1 = DATestsHelper.CreateTopic(forum, writer, "UnittestShortname1");
            DATestsHelper.CreateMessage(topic1, writer);
            //Topic topic2 = DATestsHelper.CreateTopic(forum, writer, "UnittestShortname2");
            //DATestsHelper.CreateMessage(topic2, writer);

            List<ForumCategory> loadedList = new ForumsDataAccess().GetList(reader.Id);
            Assert.NotNull(loadedList);
            ForumCategory loadedCategory = (from c in loadedList
                                            where c.Name == category.Name
                                            select c)
                                           .FirstOrDefault();
            Assert.NotNull(loadedCategory);
            Assert.IsTrue(loadedCategory.Forums.Count == 1);

            Assert.IsTrue(loadedCategory.Forums[0].UnreadMessagesCount == 2);
        }

        [Test]
        public void NewTopicWithoutReplies()
        {
            ForumCategory category = DATestsHelper.CreateCategory("Unittestcategory");

            User writer = DATestsHelper.CreateUser("UT_writerUser", UserRole.Member);
            User reader = DATestsHelper.CreateUser("UT_readerUser", UserRole.Member);

            Forum forum = DATestsHelper.CreateForum(writer, category);

            Topic topic1 = DATestsHelper.CreateTopic(forum, writer, "UnittestShortname1");

            List<ForumCategory> loadedList = new ForumsDataAccess().GetList(reader.Id);
            Assert.NotNull(loadedList);
            ForumCategory loadedCategory = (from c in loadedList
                                            where c.Name == category.Name
                                            select c)
                                           .FirstOrDefault();
            Assert.NotNull(loadedCategory);
            Assert.IsTrue(loadedCategory.Forums.Count == 1);

            Assert.IsTrue(loadedCategory.Forums[0].UnreadMessagesCount == 1);
        }
    }
}
